home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / shar / traverse.c < prev   
C/C++ Source or Header  |  1994-08-01  |  2KB  |  100 lines

  1. /*LINTLIBRARY*/
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/dir.h>
  5.  
  6. #ifdef MAXNAMLEN
  7.  
  8. #define    namedir(entry) (entry->d_name)
  9. #define    MAXNAME 256
  10.  
  11. #else
  12.  
  13. #define    DIR    FILE
  14. #define    MAXNAME (DIRSIZ+2)
  15. #define    opendir(path) fopen (path, "r")
  16. #define closedir(dirp) fclose (dirp)
  17. struct direct *
  18. readdir (dirp)
  19. DIR     *dirp;
  20.     {
  21.     static    struct    direct    entry;
  22.     if (dirp == NULL) return (NULL);
  23.     for (;;)
  24.         {
  25.         if (fread (&entry, sizeof (struct direct), 1, dirp) == 0) return (NULL);
  26.         if (entry.d_ino) return (&entry);
  27.         }
  28.     }
  29. char    *strncpy ();
  30. char *
  31. namedir (entry)
  32. struct    direct    *entry;
  33.     {
  34.     static    char    name[MAXNAME];
  35.     return (strncpy (name, entry->d_name, DIRSIZ));
  36.     }
  37.  
  38. #endif
  39.  
  40. #include <sys/stat.h>
  41. #define    isdir(path) (stat(path, &buf) ? 0 : (buf.st_mode&S_IFMT)==S_IFDIR)
  42.  
  43. traverse (path, func)
  44. char    *path;
  45. int     (*func) ();
  46.     {
  47.     DIR     *dirp;
  48.     struct    direct    *entry;
  49.     struct    stat    buf;
  50.     int     filetype = isdir (path) ? 'd' : 'f';
  51.     (*func) (path, filetype, 0);
  52.     if (filetype == 'd')
  53.         {
  54.         if (chdir (path) == 0)
  55.             {
  56.             if (dirp = opendir ("."))
  57.                 {
  58.                 while (entry = readdir (dirp))
  59.                     {
  60.                     char    name[MAXNAME];
  61.                     (void) strcpy (name, namedir (entry));
  62.                     if (strcmp(name, ".") && strcmp(name, ".."))
  63.                         traverse (name, func);
  64.                     }
  65.                 (void) closedir(dirp);
  66.                 }
  67.             (void) chdir ("..");
  68.             }
  69.         }
  70.     (*func) (path, filetype, 1);
  71.     }
  72.  
  73. #ifdef STANDALONE
  74.  
  75. static    Indent = 0;
  76. tryverse (file, type, pos)
  77. char    *file;
  78.     {
  79.     int     in;
  80.     if (pos == 0)
  81.         {
  82.         for (in = 0; in < Indent; in++) putchar ('\t');
  83.         if (type == 'd')
  84.             {
  85.             printf ("%s/\n", file);
  86.             Indent++;
  87.             }
  88.         else puts (file);
  89.         }
  90.     else if (type == 'd') Indent--;
  91.     }
  92.  
  93. main (argc, argv) char **argv;
  94.     {
  95.     int     tryverse ();
  96.     char    *root = argc > 1 ? argv[1] : ".";
  97.     traverse (root, tryverse);
  98.     }
  99. #endif
  100.